package com.um.jdy.platform.base.manager;


import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.URLUtil;
import cn.hutool.http.HttpUtil;
import cn.hutool.json.JSONUtil;
import com.um.jdy.common.base.entity.po.Area;
import com.um.jdy.common.base.service.AreaService;
import com.um.jdy.platform.base.entity.dto.AddressParam;
import com.um.jdy.platform.base.entity.vo.AreaTreeNode;
import com.um.springboot.starter.orm.entity.Response;
import com.um.springboot.starter.orm.manager.BaseManager;
import com.um.springboot.starter.service.MapService;
import com.um.springboot.starter.utils.StringUtil;
import com.um.springboot.starter.utils.UMBeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import javax.servlet.http.HttpServletRequest;
import java.util.*;

/**
 * 管理类：地区表
 */
@Component
public class AreaManager extends BaseManager<AreaService> {

    @Autowired
    HttpServletRequest request;

   @Autowired
   MapService mapService;


    public Response<List<AreaTreeNode>> getAreaTree() {


        List<Area> listProvince = service.getList("deep","1","sort_no","asc");
        List<Area> listCity = service.getList("deep","2","sort_no","asc");;
        List<Area> listArea = service.getList("deep","3","sort_no","asc");

        Map<String,List<AreaTreeNode>> mapCityChildren = new Hashtable<>();
        for(Area area : listArea){

            AreaTreeNode areaNode = UMBeanUtils.copyBean(area,AreaTreeNode.class);
            String parentId = areaNode.getBaseAreaParentId();

            List list = null;
            if(mapCityChildren.containsKey(parentId)){
                list = mapCityChildren.get(parentId);
            }else{
                list = new ArrayList();
                mapCityChildren.put(parentId,list);
            }
            list.add(areaNode);
        }

        Map<String,List<AreaTreeNode>> mapProvinceChildren = new Hashtable<>();
        for(Area city : listCity){
            AreaTreeNode cityNode = UMBeanUtils.copyBean(city,AreaTreeNode.class);
            String id = cityNode.getBaseAreaId();
            if(mapCityChildren.containsKey(id)){
                cityNode.setChildren(mapCityChildren.get(id));
            }else{
                cityNode.setChildren(new ArrayList());
            }

            String parentId = cityNode.getBaseAreaParentId();
            List list = null;
            if(mapProvinceChildren.containsKey(parentId)){
                list = mapProvinceChildren.get(parentId);
            }else{
                list = new ArrayList();
                mapProvinceChildren.put(parentId,list);
            }
            list.add(cityNode);

        }

        List<AreaTreeNode> listProvinceNode = new ArrayList<>();
        for(Area province : listProvince){
            AreaTreeNode provinceNode = UMBeanUtils.copyBean(province,AreaTreeNode.class);
            String id = provinceNode.getBaseAreaId();
            if(mapProvinceChildren.containsKey(id)){
                provinceNode.setChildren(mapProvinceChildren.get(id));
            }else{
                provinceNode.setChildren(new ArrayList());
            }
            listProvinceNode.add(provinceNode);
        }
        return Response.success(listProvinceNode);
    }

    /**
     * 关键词输入提示
     * @param param
     * @return
     */
    public Response searchAddress(AddressParam param){
        return Response.success(mapService.searchAddress(param.getKeyword(),param.getRegion()));
    }


//    public Response searchAddress(AddressParam param){
//        Map condition = new Hashtable();
//        condition.put("key", MAP_KEY);
//        condition.put("page_index", param.getPageNo());
//        condition.put("page_size", param.getPageSize());
//        condition.put("keyword", URLUtil.encode(param.getKeyword()));
//        condition.put("boundary", String.format("nearby(%s,%s,%s)", param.getLatitude(), param.getLatitude(), 100000000));
//        condition.put("orderby", "_distance");
//
//        cn.hutool.json.JSONObject result = JSONUtil.parseObj(HttpUtil.get(SEARCH_API, condition));
//        return Response.success(result);
//    }

//    public Response inverseGeocode(AddressParam param){
//
//        Map condition = new HashMap();
//        condition.put("location", String.format("%s,%s", param.getLatitude(), param.getLongitude()));
//        condition.put("key", MAP_KEY);
//
//        cn.hutool.json.JSONObject result = JSONUtil.parseObj(HttpUtil.get(GEOCODER_API, condition));
//        return Response.success(result);
//    }

//    public Response getLocation(){
//        String ipAddr = getIpAddr(request);
//
//        Map condition = new HashMap();
//        condition.put("ip", ipAddr);
//        condition.put("key", MAP_KEY);
//
//        cn.hutool.json.JSONObject result = JSONUtil.parseObj(HttpUtil.get(LOCATION_API, condition));
//        return Response.success(result);
//    }
//
//    public String getIpAddr(HttpServletRequest request) {
//        String ip = request.getHeader("x-forwarded-for");
//        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
//            ip = request.getHeader("Proxy-Client-IP");
//        }
//        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
//            ip = request.getHeader("WL-Proxy-Client-IP");
//        }
//        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
//            ip = request.getRemoteAddr();
//        }
//        return ip;
//
//    }

}
